package com.app.service;

import com.alibaba.fastjson.JSON;
import com.app.dao.mysql.YjDataMapper;
import com.app.domain.UploadParameter;
import com.app.domain.YjData;
import com.app.domain.jgaeb.LoginCookie;
import com.app.domain.jgaeb.PostGroutingResult;
import com.app.domain.sneb.Grouting;
import com.app.domain.sneb.HoleGrouting;
import com.app.utils.HttpClients;
import com.app.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class SnebService {
    @Autowired
    UploadParameterService uploadParameterService;
    @Autowired
    YjDataService yjDataService;
    @Autowired
    YjDataMapper yjDataMapper;


    public Grouting getGrouting(List<YjData> yjDataList, UploadParameter uploadParameter, String projectpid, String beamfieldname) {
        Grouting grouting = new Grouting();
        grouting.setProjectpid(projectpid);
        grouting.setBeamfieldname(beamfieldname);
        grouting.setBridgename(uploadParameter.getHtbh());
        grouting.setBeamcode(yjDataList.get(0).getBridge_code());
        grouting.setBeamtype(uploadParameter.getLblx());
        grouting.setBeamholenum(yjDataList.size());
        grouting.setBeamlength(Float.parseFloat(uploadParameter.getHolelength()));
        grouting.setYjtype(uploadParameter.getYajiangmoshi());
        grouting.setFluidity(yjDataList.get(0).getPractical_fluidity());
        grouting.setStepname(uploadParameter.getYajiangbuzhou());
        grouting.setCementname("常規水泥");
        grouting.setGroagenname(uploadParameter.getYajiangji());
        grouting.setGroaspect(uploadParameter.getYajiangfangxiang());
        grouting.setInitialfluidity(Float.parseFloat(yjDataList.get(0).getPractical_fluidity()));
        grouting.setQuainspector("常规");
        grouting.setSupervisors("常规");
        grouting.setConoperator("常规");
        grouting.setGrodevcode(yjDataList.get(0).getDevice_code());
        List<HoleGrouting> holeGroutingList = getHoleGrouting(yjDataList, uploadParameter);
        grouting.setYjData(holeGroutingList);
        return grouting;
    }

    private List<HoleGrouting> getHoleGrouting(List<YjData> yjDataList, UploadParameter uploadParameter) {
        List<HoleGrouting> holeGroutingList = new ArrayList<>();
        int count = 1;
        for (YjData yjData : yjDataList) {
            HoleGrouting holeGrouting = new HoleGrouting();
            holeGrouting.setSteelcode(yjData.getDuct_code());
            holeGrouting.setYjtype(uploadParameter.getYajiangmoshi());
            holeGrouting.setYjdate(yjData.getMudjack_date() + " " + yjData.getBegin_time());
            holeGrouting.setMixturepro(uploadParameter.getPhb());
            holeGrouting.setWaterbinderratio(String.valueOf(yjData.getWater_binder_ratio()));
            holeGrouting.setStirtime(uploadParameter.getStirtime());
            holeGrouting.setStarttime(yjData.getMudjack_date() + " " + yjData.getBegin_time());
            holeGrouting.setEndtime(yjData.getMudjack_date() + " " + yjData.getEnd_time());
            holeGrouting.setInpressure(String.valueOf(yjData.getMudjack_pressure()));
            holeGrouting.setAntipressure(String.valueOf(yjData.getSlurry_pressure()));
            holeGrouting.setPressuretime(yjData.getPressurize_time());
            holeGrouting.setInpulpvolume(String.valueOf(yjData.getPractical_volume()));
            holeGrouting.setPulpvolumedv(String.valueOf(yjData.getTheory_volume()));
            holeGrouting.setSlurrytemp(String.valueOf(yjData.getSlurry_temperature()));
            holeGrouting.setEhstemp(String.valueOf(yjData.getEnvironment_temperature()));
            holeGrouting.setSeqnumber(String.valueOf(count));
            holeGroutingList.add(holeGrouting);
            count++;

        }
        return holeGroutingList;
    }

    public String sendMessage(String serviceUrl, String json) {
        String result = HttpClients.requestPost(serviceUrl, json);
        return result;
    }
}
